function getUrl(){
    var url = window.location.href;
    var path = window.location.pathname;
    var search = window.location.search;
    url = url.replace(path,"/");
    url = url.replace(search,"")
    return url;
}

$(document).ready(function () {

    const url = getUrl();

    $.ajax({
        async : false,
        url:url+"adminInfo/listCollege",
        data:{},
        type: "get",
        contentType: "application/json",
        success: function(map) {
            console.log(map);

            data = map.data;

            if(map=="unLogin"){
                location.href="login.html";
            }else{
                $.each(map, function (i, college) {
                    console.log(college);

                    const option = '<option'+' value ="'+college.id+'">'+college._name+'</option>';

                    const $obj = $(option);

                    $("#college").append($obj);
                })
                console.log(map[0]);

                $.ajax({
                    async : false,
                    url:url+"adminInfo/getMajor",
                    data:{
                        index:map[0].id
                    },
                    type: "get",
                    contentType: "application/json",
                    success: function(map) {
                        console.log(map);
            
                        data = map.data;
            
                        if(map=="unLogin"){
                            location.href="login.html";
                        }else{
                            $.each(map, function (i, major) {
                                console.log(major);
            
                                const option = '<option'+' value ="'+major.id+'">'+major._name+'</option>';
            
                                const $obj = $(option);
            
                                $("#major").append($obj);
                            })

                            $.ajax({
                                url:url+"adminInfo/getClass",
                                data:{
                                    index:map[0].id
                                },
                                type: "get",
                                contentType: "application/json",
                                success: function(map) {
                                    console.log(map);
                        
                                    data = map.data;
                        
                                    if(map=="unLogin"){
                                        location.href="login.html";
                                    }else{
                                        $.each(map, function (i, _class) {
                                            console.log(_class);
                        
                                            const option = '<option'+' value ="'+_class.id+'">'+_class._name+'</option>';
                        
                                            const $obj = $(option);
                        
                                            $("#_class").append($obj);
                                        })
                                    }
                                },
                                error:function(){
                                    console.log("----------")
                                }
                            })

                        }
                    },
                    error:function(){
                        console.log("----------")
                    }
                })


            }
        },
        error:function(){
            console.log("----------")
        }
    })

    $("#college").change(function() {
        const id = $("#college").val();
        console.log(id);

        $("#major").empty();
        $("#_class").empty();

        $.ajax({
            async : false,
            url:url+"adminInfo/getMajor",
            data:{
                index:id
            },
            type: "get",
            contentType: "application/json",
            success: function(map) {
                console.log(map);
    
                data = map.data;
    
                if(map=="unLogin"){
                    location.href="login.html";
                }else{
                    $.each(map, function (i, major) {
                        console.log(major);
    
                        const option = '<option'+' value ="'+major.id+'">'+major._name+'</option>';
    
                        const $obj = $(option);
    
                        $("#major").append($obj);
                    })

                    $.ajax({
                        url:url+"adminInfo/getClass",
                        data:{
                            index:map[0].id
                        },
                        type: "get",
                        contentType: "application/json",
                        success: function(map) {
                            console.log(map);
                
                            data = map.data;
                
                            if(map=="unLogin"){
                                location.href="login.html";
                            }else{
                                $.each(map, function (i, _class) {
                                    console.log(_class);
                
                                    const option = '<option'+' value ="'+_class.id+'">'+_class._name+'</option>';
                
                                    const $obj = $(option);
                
                                    $("#_class").append($obj);
                                })
                            }
                        },
                        error:function(){
                            console.log("----------")
                        }
                    })

                }
            },
            error:function(){
                console.log("----------")
            }
        })
    });


    $("#major").change(function() {
        const id = $("#major").val();
        console.log(id);

        $("#_class").empty();

        $.ajax({
            async : false,
            url:url+"adminInfo/getClass",
            data:{
                index:id
            },
            type: "get",
            contentType: "application/json",
            success: function(map) {
                console.log(map);
    
                data = map.data;
    
                if(map=="unLogin"){
                    location.href="login.html";
                }else{
                    $.each(map, function (i, _class) {
                        console.log(_class);
    
                        const option = '<option'+' value ="'+_class.id+'">'+_class._name+'</option>';
    
                        const $obj = $(option);
    
                        $("#_class").append($obj);
                    })
                }
            },
            error:function(){
                console.log("----------")
            }
        })
    })

})

$("#submit").click(function () {
    
    const url = getUrl();

    const map ={
        "number":$("#number").val(),
        "name":$("#name").val(),
        "sex":$("#sex").val(),
        "idNumber":$("#idNumber").val(),
        "birth":$("#birth").val(),
        "nation":$("#nation").val(),
        "place":$("#province").val()+$("#city").val(),
        "postCode":$("#postCode").val(),
        "status":$("#status").val(),
        "phone":$("#phone").val(),
        "mail":$("#mail").val(),
        "studentType":$("#studentType").val(),
        "studentProvince":$("#studentProvince").val(),
        "middleSchool":$("#middleSchool").val(),
        "date":$("#date").val(),
        "_class":$("#_class").find("option:selected").text(),
        "level":$("#level").val(),
        "college":$("#college").find("option:selected").text(),
        "major":$("#major").find("option:selected").text()
    }

    console.log(map);


    $.ajax({
        url:url+"adminInfo/addInfo",
        data:JSON.stringify(map),
        type: "post",
        contentType: "application/json; charset=utf-8",
        success: function(data) {
            console.log(data);
            location.href="listInfo.html";
        },
        error:function(data){
            console.log("----------");
            console.log(data);
        }
    })

})

$("#upload").click(function () {
    const url = getUrl();
    var type = "file";          //后台接收时需要的参数名称，自定义即可
    var formData = new FormData();
    formData.append(type, $("#fileInput")[0].files[0]);    //生成一对表单属性
    console.log(formData);
    $.ajax({
        type: "POST",           //因为是传输文件，所以必须是post
        url: url+"adminInfo/upload",         //对应的后台处理类的地址
        data: formData,
        processData: false,
        contentType: false,
        success: function (data) {
            console.log(data);
            location.href="listInfo.html";
        }
    });
})